package com.magiccompass.alipay.barcode.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class DateTimeUtil {
	public static Timestamp getCurrentTimestamp(){
		Date date= new Date();
		return new Timestamp(date.getTime());
	}
	
	public static Timestamp getYesterdayStartTime(){
		Calendar yesterdayCalendar = Calendar.getInstance();
		yesterdayCalendar.add(Calendar.DATE, -1);
		yesterdayCalendar.set(Calendar.HOUR_OF_DAY, 0);
		yesterdayCalendar.set(Calendar.MINUTE, 0);
		yesterdayCalendar.set(Calendar.SECOND, 0);
		yesterdayCalendar.set(Calendar.MILLISECOND, 0);
		return new Timestamp(yesterdayCalendar.getTime().getTime());
	}
	
	public static Timestamp getYesterdayEndTime(){
		Calendar yesterdayCalendar = Calendar.getInstance();
		yesterdayCalendar.add(Calendar.DATE, -1);
		yesterdayCalendar.set(Calendar.HOUR_OF_DAY, 23);
		yesterdayCalendar.set(Calendar.MINUTE, 59);
		yesterdayCalendar.set(Calendar.SECOND, 59);
		yesterdayCalendar.set(Calendar.MILLISECOND, 999);
		return new Timestamp(yesterdayCalendar.getTime().getTime());
	}
	
	public static String getYesterdayYYYYMMDDString(){
		Calendar yesterdayCalendar = Calendar.getInstance();
		yesterdayCalendar.add(Calendar.DATE, -1);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = yesterdayCalendar.getTime();
		return sdf.format(date);
	}
	
	public static String getYesterdayDDMMYYYYString(){
		Calendar yesterdayCalendar = Calendar.getInstance();
		yesterdayCalendar.add(Calendar.DATE, -1);
		SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
		Date date = yesterdayCalendar.getTime();
		return sdf.format(date);
	}
	
	public static String getGMTDateString(Date inputDate){
		String returnStr = null;
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
	    try {
	    	returnStr = sdf.format(inputDate);
		} catch (Exception e) {
			returnStr = null;
			e.printStackTrace();
		}
	    return returnStr;
	}
	
	public static String getCurrentDateYYYYMMDDHH24MISSString(){
		String returnStr = null;
		Date date= new Date();
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
	    try {
	    	returnStr = sdf.format(date);
		} catch (Exception e) {
			returnStr = null;
			e.printStackTrace();
		}
	    return returnStr;
	}
	
	public static String getYYYYMMDDHH24MISSStringFromTimestamp(Timestamp ts){
		String returnStr = null;
		Date date= new Date(ts.getTime());
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
	    try {
	    	returnStr = sdf.format(date);
		} catch (Exception e) {
			returnStr = null;
			e.printStackTrace();
		}
	    return returnStr;
	}
	
	public static Timestamp getTimestampFromStringByFormat(String timeStr, String format){
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			Date date = sdf.parse(timeStr);
			return new Timestamp(date.getTime());
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static String formatTimeByFormat(Timestamp time, String format){
		if(time == null){
			return null;
		}else{
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			try {
				String dateStr = sdf.format(new Date(time.getTime()));
				return dateStr;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}		
	}
	
	public static void main(String[] args){
		System.out.println(getTimestampFromStringByFormat("2016-03-02","yyyy-MM-dd"));
	}
}
